<template>
    <div>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="username" label="姓名"  >
            </el-table-column>
            <el-table-column prop="email" label="邮箱"  >
            </el-table-column>
            <el-table-column prop="phone" label="电话"  >
            </el-table-column>
            <el-table-column prop="state" label="状态"  >
            </el-table-column>
            <el-table-column prop="age" label="年龄"  >
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination  @size-change="handleSizeChange" layout="sizes,prev, pager, next" @current-change="handleCurrentChange" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="姓名" prop="username">
                    <el-input v-model="addForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input-number v-model="addForm.age" ></el-input-number>
                </el-form-item>
                <el-form-item label="email">
                    <el-input v-model="addForm.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话">
                    <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "employee",
        data(){
            return{
                employees:[],
                listLoading:true,//加载效果
                filters:{},
                total:0,
                sels:[],
                currPage:1,
                pageSize:10,
                addFormVisible:false,
                addForm:{
                    username:'',
                    age:0,
                    phone:'',
                    state:1,
                    email:''

                },
                addLoading:false,
                addFormRules:{//规则详情
                    username: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' },
                        { min: 2, message: '部门名称不能小于两个字符', trigger: 'blur' }
                    ],
                },
            }

        },
        methods:{
            addSubmit(){
                //打开加载效果
                this.addLoading=true;
                this.$refs.addForm.validate((valid) => {
                    if (valid) {//如果通过验证  发起后台的请求添加数据
                        this.$http.put("/emp", this.addForm).then((res) => {
                            let {success, message} = res.data;
                            if (success) {
                                this.$message({
                                    message: message,
                                    type: 'success'
                                });
                            } else {
                                this.$message({
                                    message: message,
                                    type: 'error'
                                });
                            }
                            //关闭 模态框
                            this.addFormVisible = false;
                            this.addLoading=false;
                            //重新加载列表
                            this.loadListData();
                        })
                    }

                })
            },
            handleSizeChange(val){//监听用户选择哪一个展示的值
                this.pageSize=val;
                //重新发起请求加载数据
                this.loadListData();
            },
            handleCurrentChange(val){
                this.currPage=val;
                this.loadListData();
            },
            batchRemove(v){

            },
            handleAdd(){
                //清空表单
                for( var i in this.addForm){
                      this.addForm[i]='';
                }
                this.addForm.state=1;
                this.addFormVisible=true;

            },
            search(){
                //把当前页改为第一页
                this.currPage=1
                this.loadListData();

            },
            selsChange(){

            },
            handleEdit(index,row){
                //回显数据
                for( var i in this.addForm){
                    this.addForm[i]=row[i];
                }
                this.addForm.id=row.id;
                this.addFormVisible=true;

            },
            handleDel(index,row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(()=>{
                    this.listLoading=true;
                    //发起后台的请求  删除数据
                    this.$http.delete("/emp/"+row.id).then((res)=>{
                        //解构表达式
                        let {success,message}=res.data;
                        if(success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: '网络异常',
                                type: 'error'
                            });
                        }
                        //重新加载列表
                        this.loadListData();
                    })

                })
            },
            loadListData(){
                let param={
                    currentPage:this.currPage,
                    keywords:this.filters.name,
                    pageSize:this.pageSize
                }
                this.$http.post("/emp",param).then((res)=>{
                    this.employees=res.data.data;
                    this.total=res.data.total;
                })
                this.listLoading=false;

            }

        },
        mounted(){
            this.loadListData();
        }
    }
</script>

<style scoped>

</style>